package work.mediway.common.transport;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/7/6
 */
@Getter
@ToString
public class ErrorMsgVO {

    private Integer total = 0;

    private Integer success = 0;

    private Integer fail = 0;

    private List<ErrorBase> error = CollUtil.newArrayList();

    public ErrorMsgVO() {
        fail = 0;
    }

    public void setCount(int total) {
        this.total = total;
        this.success = this.total - this.fail;
    }

    public void setCount(int total, int fail) {
        this.total = total;
        this.fail = fail;
        this.success = this.total - this.fail;
    }

    private void incrementFail() {
        this.fail = ++this.fail;
    }

    public void setErrorMsg(Object data, String msg) {
        this.error.add(new ErrorBase(data, msg));
        incrementFail();
    }

    public void setErrorMsgNoIncrmnent(Object data, String msg) {
        this.error.add(new ErrorBase(data, msg));
    }

    public void addError(ErrorMsgVO msgVO) {
        this.total = this.total + msgVO.getTotal();
        this.success = this.success + msgVO.getSuccess();
        this.fail = this.fail + msgVO.getFail();
        this.error.addAll(msgVO.getError());
    }

    @Data
    public static class ErrorBase {
        private Object data;

        private String msg;

        public ErrorBase(Object obj, String msg) {
            this.data = obj;
            this.msg = msg;
        }
    }

}
